package listadmin;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub total_list_admin {
    my $self = shift;

    my $sth =
      $self->{dbh}->prepare("SELECT count(*) FROM users where id > '5'");
    $sth->execute();
    $self->{p}->{total_number} = $sth->fetchrow_array();
    $sth->finish();
    return $self;
}

sub page_list_admin {
    my $self = shift;
    $self->{p}->{file} = 'listadmin';
    $self->SUPER::page_list(@_);
    return $self;
}

sub return_list_admin {
    my $self = shift;

    my $sth = $self->{dbh}->prepare(
        " 
                         SELECT t1.id,
                                t1.name,
                                t1.info,
                                t1.active,
                                t1.created,
                                t2.m_mailbox,
                                t2.m_domain,
                                t2.m_alias,
                                t2.mb_domain,
                                t2.mb_mailbox,
                                (SELECT count(*) from domain where idnane = t1.id) AS c_domain
                        FROM users  AS t1
                        LEFT JOIN admin_domain AS t2
                        ON t1.id = t2.idname
                        WHERE  t1.id > '5'
                        ORDER BY id desc
                        LIMIT ?, ?
                        
                             "
    );
    $sth->execute( $self->{p}->{jj}, $self->{se}->{line_from_page} );

    my $loop_data;

    push @{$loop_data}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    foreach $_ ( @{$loop_data} ) {
        $_->{active} = $_->{active} == 1 ? 'YES' : 'NO';
        $_->{info}   = $_->{info} == 1   ? 'YES' : 'NO';

        $_->{m_alias} = 'Disable'   if ( $_->{m_alias} eq '-1' );
        $_->{m_alias} = 'Unlimited' if ( $_->{m_alias} eq '0' );
        $_->{m_alias} = '-'         if ( $_->{m_alias} eq '' );

        $_->{m_mailbox} = 'Unlimited' if ( $_->{m_mailbox} eq '0' );
        $_->{m_mailbox} = '-'         if ( $_->{m_mailbox} eq '' );

        $_->{m_domain} = 'Unlimited' if ( $_->{m_domain} eq '0' );
        $_->{m_domain} = '-'         if ( $_->{m_domain} eq '' );
        $_->{from} = $self->{'p'}->{'from'} if ( $self->{'p'}->{'from'} );

        if ( $_->{mb_domain} eq '0' ) {
            $_->{mb_domain} = 'Unlimited';
        }
        elsif ( $_->{mb_domain} eq '' ) {
            $_->{mb_domain} = '-';
        }
        else {
            $_->{mb_domain} = sprintf( '%.1f', $_->{mb_domain} / 1048576 );
        }

        if ( $_->{mb_mailbox} eq '0' ) {
            $_->{mb_mailbox} = 'Unlimited';
        }
        elsif ( $_->{mb_mailbox} eq '' ) {
            $_->{mb_mailbox} = '-';
        }
        else {
            $_->{mb_mailbox} = sprintf( '%.1f', $_->{mb_mailbox} / 1048576 );
        }
    }

    if ( !$loop_data ) {
        $self->{he}->{NOTS} = 1;
    }
    else {
        $self->{he}->{ROWS} = $loop_data;
    }

    return $self;
}

sub templ_hash_admin_list {
    my $self = shift;

    $self->{session}->{filet} = 'listadmin';

    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
